package com.apps.ftapp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.apps.ftapp.R;
import com.apps.ftapp.db.Kj;
import com.apps.ftapp.db.Lesson;
import com.apps.ftapp.db.LessonChild;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.SDCardUtils;
import com.apps.ftapp.util.Tools;
import com.duobeiyun.util.FileUtil;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;
import me.leefeng.promptlibrary.PromptDialog;

/**
 * 设置
 */
public class SetActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.set_cache)
    TextView setCache;
    @BindView(R.id.set_switch)
    TextView setSwitch;
    @BindView(R.id.set_switch_img)
    ImageView setSwitchImg;
    @BindView(R.id.filesize)
    TextView filesize;
    @BindView(R.id.qingli)
    RelativeLayout qingli;

    private PromptDialog promptDialog;

    private String dirName = SDCardUtils.getSDCardPath() + "duobeiyun/";

    private List<Lesson> lessonList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);

        setTitle("设置");

        setSwitchImg.setOnClickListener(this);
        qingli.setOnClickListener(this);


        double total = 0.0;
        lessonList = DataSupport.findAll(Lesson.class);
        if (null != lessonList && lessonList.size() > 0) {
            for (Lesson lesson : lessonList) {
                L.i("main", lesson.getSizes().replace("mb", ""));
                total += Tools.isDoubleNull(lesson.getSizes().replace("mb", ""));
            }

            List<Kj> lessonList1 = DataSupport.findAll(Kj.class);
            if (null != lessonList1 && lessonList1.size() > 0) {
                for (Kj l : lessonList1) {
                    L.i("main", l.getSizes().replace("MB", ""));
                    if (l.getSizes().contains("MB"))
                        total += Tools.isDoubleNull(l.getSizes().replace("MB", ""));

                }
            }

            filesize.setText(total + "M");
        } else {
            filesize.setText(total + "M");
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_switch_img:
                chan(v);
                break;
            case R.id.qingli:
                new MaterialDialog.Builder(this)
                        .title(R.string.title1)
                        .content(R.string.clear_cache)
                        .contentColor(Color.BLACK)
                        .positiveText(R.string.agree)
                        .positiveColor(Color.BLACK)
                        .negativeColor(Color.BLACK)
                        .negativeText(R.string.disagree)
                        .onAny(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                if (which == DialogAction.NEUTRAL) {
                                } else if (which == DialogAction.POSITIVE) {
                                    //清理
                                    promptDialog = new PromptDialog(SetActivity.this);
                                    promptDialog.showLoading("清理中..");
                                    FileUtil.deleteFile(dirName);
                                    //删除数据库数据
                                    DataSupport.deleteAll(LessonChild.class);
                                    DataSupport.deleteAll(Lesson.class);
                                    DataSupport.deleteAll(Kj.class);
                                    //删除数据库数据
                                    promptDialog.dismiss();

                                    filesize.setText("0.0M");

                                    Toasty.normal(getApplicationContext(), "清理完成〜").show();
//                          Toast.makeText(MainActivity.this, "同意" + dataChoose, Toast.LENGTH_LONG).show();
                                } else if (which == DialogAction.NEGATIVE) {
                                }

                            }
                        })
                        .show();


                break;
        }
    }


    private void chan(View v) {

        int tag = Tools.isIntNull(v.getTag());
        if (0 == tag) {//0 当前是开启
            setSwitchImg.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.guanbi));
            setSwitchImg.setTag(1);
            Toasty.normal(getApplicationContext(), "提醒关闭").show();
        } else {
            setSwitchImg.setTag(0);
            setSwitchImg.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.kaiqi));
            Toasty.normal(getApplicationContext(), "提醒开启").show();
        }
    }
}
